import { Color, Mesh } from "three";

function setRandomColors(object: any, scale: any) {
  let children = object.children;

  if (children && children.length > 0) {
    children.forEach(function (e: any) {
      setRandomColors(e, scale)
    });
  } else {
    // no children assume contains a mesh
    if (object instanceof Mesh) {
      if (object.material instanceof Array) {
        object.material.forEach(function(m) {
          m.color = new Color(scale(Math.random()).hex());
          if (m.name.indexOf("building") == 0) {
              m.emissive = new Color(0x444444);
              m.transparent = true;
              m.opacity = 0.8;
            }
        });
      } else {
        object.material.color = new Color(scale(Math.random()).hex());
        if (object.material.name.indexOf("building") == 0) {
          object.material.emissive = new Color(0x444444);
          object.material.transparent = true;
          object.material.opacity = 0.8;
        }
      }
    }
  }
}

export default setRandomColors